package com.woniu.woniuk16.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.woniuk16.commens.ComUtil;
import com.woniu.woniuk16.commens.Menu;
import com.woniu.woniuk16.dto.MenuDto;
import com.woniu.woniuk16.dto.RuleSelMenusDto;
import com.woniu.woniuk16.entity.WoniuAccount;
import com.woniu.woniuk16.entity.WoniuAccountRule;
import com.woniu.woniuk16.entity.WoniuMenus;
import com.woniu.woniuk16.entity.WoniuRuleMenus;
import com.woniu.woniuk16.mapper.WoniuMenusMapper;
import com.woniu.woniuk16.param.ChangeRuleParam;
import com.woniu.woniuk16.service.WoniuAccountRuleService;
import com.woniu.woniuk16.service.WoniuAccountService;
import com.woniu.woniuk16.service.WoniuMenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.woniuk16.service.WoniuRuleMenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-17
 */
@Service
public class WoniuMenusServiceImpl extends ServiceImpl<WoniuMenusMapper, WoniuMenus> implements WoniuMenusService {
    @Autowired
    private WoniuAccountRuleService woniuAccountRuleService;
    @Autowired
    private WoniuRuleMenusService woniuRuleMenusService;
    @Autowired
    private WoniuAccountService woniuAccountService;
    @Override
    public List<MenuDto> getMenus(Integer id) {

        QueryWrapper<WoniuAccountRule> war = new QueryWrapper<>();
        war.eq("account_id",id);
        List<WoniuAccountRule> list = woniuAccountRuleService.list(war);
        List<Integer> ids = list.stream().map(WoniuAccountRule::getRuleId).collect(Collectors.toList());
        QueryWrapper<WoniuRuleMenus> wrms = new QueryWrapper<>();
        wrms.in("rule_id",ids);
        List<WoniuRuleMenus> list1 = woniuRuleMenusService.list(wrms);
        List<Integer> collect = list1.stream().map(WoniuRuleMenus::getMenusId).collect(Collectors.toList());
        QueryWrapper<WoniuMenus> wm = new QueryWrapper<>();
        wm.in("menus_id",collect);
        List<WoniuMenus> list2 = list(wm);
        List<MenuDto> menuDtos = formatToTree(list2);
        return menuDtos;
    }

    @Override
    public List<MenuDto> getMenusByRule(Integer id) {
        QueryWrapper<WoniuRuleMenus> wrms = new QueryWrapper<>();
        wrms.eq("rule_id",id);
        List<WoniuRuleMenus> list1 = woniuRuleMenusService.list(wrms);
        List<Integer> collect = list1.stream().map(WoniuRuleMenus::getMenusId).collect(Collectors.toList());
        if (collect==null||collect.size()<=0){
            return new ArrayList<MenuDto>();
        }
        QueryWrapper<WoniuMenus> wm = new QueryWrapper<>();
        wm.in("menus_id",collect);
        List<WoniuMenus> list2 = list(wm);
//        List<MenuDto> menuDtos = formatToTree(list2);
        List<MenuDto> menuDtos = ComUtil.copyList(list2, MenuDto.class);
        return menuDtos;
    }


    @Override
    public List<MenuDto> getByUserId(Integer id) {
        Integer accountId = woniuAccountService.getOne(new QueryWrapper<WoniuAccount>().eq("user_id", id)).getAccountId();
        List<MenuDto> menus = getMenus(accountId);
        return menus;
    }

    private List<MenuDto> formatToTree(List<WoniuMenus> list) {
        List<MenuDto> menuDtos = ComUtil.copyList(list, MenuDto.class);
        Map<Integer, MenuDto> maps = new HashMap<>();
        for (int i = 0;i<menuDtos.size();i++) {
            maps.put(menuDtos.get(i).getMenusId(),menuDtos.get(i));
        }
        List<MenuDto> results = new ArrayList<MenuDto>();
        for (int i = 0;i<menuDtos.size();i++) {
            if (menuDtos.get(i).getMenusParentid()==0){
                results.add(menuDtos.get(i));
            }else{
                if (maps.get(menuDtos.get(i).getMenusParentid()).getMenuDtos()==null){
                    maps.get(menuDtos.get(i).getMenusParentid()).setMenuDtos(new ArrayList<MenuDto>());
                }
                maps.get(menuDtos.get(i).getMenusParentid()).getMenuDtos().add(menuDtos.get(i));
            }
        }
        return results;
    }


    public RuleSelMenusDto getRuleMenu(Integer id){
        List<WoniuMenus> list = list();
        List<MenuDto> menuDtos = formatToTree(list);
        List<MenuDto> menusByRule = getMenusByRule(id);
        RuleSelMenusDto ruleSelMenusDto = new RuleSelMenusDto();

        if (menusByRule==null||menusByRule.size()<=0){
            ruleSelMenusDto.setMenus(menuDtos);
            return ruleSelMenusDto;
        }
        menusByRule.removeIf(rf -> rf.getMenusType() == 0);

        List<Integer> collect = menusByRule.stream().map(me -> me.getMenusId()).collect(Collectors.toList());


        ruleSelMenusDto.setIds(collect);
        ruleSelMenusDto.setMenus(menuDtos);
        return ruleSelMenusDto;
    }

    @Override
    public boolean changeRule(ChangeRuleParam changeRuleParam) {
        List<Integer> checked = changeRuleParam.getChecked();
        Integer ruleId = changeRuleParam.getRuleId();
        QueryWrapper<WoniuRuleMenus> woniuRuleMenusQueryWrapper = new QueryWrapper<>();
        woniuRuleMenusQueryWrapper.eq("rule_id",ruleId);
        woniuRuleMenusService.remove(woniuRuleMenusQueryWrapper);

        if (checked==null||checked.size()<=0){
            return true;
        }
        ArrayList<WoniuRuleMenus> woniuRuleMenus = new ArrayList<>();
        for (Integer integer : checked) {
            woniuRuleMenus.add(WoniuRuleMenus.builder().menusId(integer).ruleId(ruleId).build());
        }
        woniuRuleMenusService.saveBatch(woniuRuleMenus);
        return true;

    }
}
